<?php

require './site/dkp/dkpfunctions.php';

if (isEditable())
{
	if (isset($_POST['setDungeonName'])) 
		addDungeon();
	if (isset($_POST['setMobName'])) 
		addMob();
	if (isset($_POST['setNewItems']))
		setItems();
}
showPageHeader();
showIntroText();
showDetail();
showPageFoot();



// -------------------------------------------------------------
//  ----  Functions  -----------------------------------------
// -------------------------------------------------------------
function showPageHeader()
/*
Opens the pages middle section content box and adds the title
*/
{
?>
    	<div id="content">
            <h2><span>DKP management tool - Beta</span></h2>
            <div class="box">
                <div class="contents">
<?php
}
// -------------------------------------------------------------
function showPageFoot()
/*
Displays final links and closes the pages middle section content box
*/
{
?>
                </div>
            </div>
			<?php 
				displayDKPLog(20); 
			?>
    	</div>
<?php
}
// -------------------------------------------------------------
function showIntroText()
/*
Displays the text at the top of the page
*/
{
?>
<div>
	<p style="padding: 5px 10px 10px 4px;">
		Tools for managing the DKP tool contents.
	</p>
</div>
<?php
}
// -------------------------------------------------------------
function showDetail()
{
	$gamesArray = getGamesArray();
	$dungeonArray = getDungeonsArray();
	$mobsArray = getMobsArray();
	$itemsArray = getItemsArray();
	if (isset($_POST['setItemIDs']))
		addItems();
	if (isset($_GET['addItem'])) {
		?>
		<form method="post" action="index.php?page=dkpmanage&d=<?php echo $_GET['d']; ?>&m=<?php echo $_GET['addItem']; ?>" name="addItems">
		<p>Add item to loot table for <b><?php echo $mobsArray[$_GET['addItem']][mobName]; ?></b></p>
		Item IDs: <input type="text" name="setItemIDs" size="50" tabindex="1"><br /><br />
		<input type="submit" value="Submit" tabindex="2"></form><br />
		<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>
		<?php
	}
	if ($_GET[action] == 'setDungeon') {
		setDungeon($gamesArray);
	} else {
		if (!isset($_GET['d'])) {
			manageGames($gamesArray, $dungeonArray);
		} else {
			manageMobs($_GET['d'], $dungeonArray[$_GET['d']][dungeonName], $mobsArray, $itemsArray);
		}
	}
}
// -------------------------------------------------------------
function setItems()
{
 	global $db, $pun_user;
	$itemDataArray = getItemDataArray($_POST['setNewItems']);
	foreach ($itemDataArray as $item) {
		$slotInfo = explode(',', getSlotInfo($item[slot]));
		$formattedName = str_replace("'", "\'", $item[name]);
		$queryString = "
			INSERT INTO 
				dmzdkp_items (MobID, Name, iLevel, SlotID, gameItemID, image)
			VALUES
				(".$_GET['m'].", '".$formattedName."', ".$item[level].", ".$slotInfo[0].", ".$item[id].", '".$item[icon]."')
			;";
//		echo $queryString.'<br />';
		$db->query($queryString) 
			or error('Unable to add items', __FILE__, __LINE__, $db->error()); 
		$queryString = "
			INSERT INTO 
				dmzdkp_items (MobID, Name, iLevel, SlotID, gameItemID, image)
			VALUES
				(".$_GET['m'].", '".$formattedName."', 0, ".$slotInfo[0].", ".$item[id].", '".$item[icon]."')
			;";
//		echo $queryString.'<br />';
		$db->query($queryString) 
			or error('Unable to add items', __FILE__, __LINE__, $db->error()); 
	}
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Add', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function addItems()
{
	$itemDataArray = getItemDataArray($_POST['setItemIDs']);
	echo '<fieldset><legend>Add the following item(s)</legend>';
	foreach ($itemDataArray as $item) {
		echo getItemDisplay($item[id], getItemColour($item['id']), $item[icon], $item[name], '?');
		$slotInfo = explode(',', getSlotInfo($item[slot]));
		echo '<br /> Slot: '.$slotInfo[1];
		echo '<br />  Item level: '.$item[level];
		echo '<br />  Mob: '.$item[mob];
		echo '<br />';
	}
	?>
	<form method="post" action="index.php?page=dkpmanage&d=<?php echo $_GET['d']; ?>&m=<?php echo $_GET['m']; ?>" name="setNewItems">
		<input type="hidden" name="setNewItems" value="<?php echo $_POST['setItemIDs']; ?>">
		<p style="text-align: right"><input type="submit" value="Submit"></p>
	</form>
	<?php
	echo '<br /></fieldset>';
}
// -------------------------------------------------------------
function setDungeon($gamesArray)
{
	echo '<form method="post" action="index.php?page=dkpmanage" name="addDungeon">';
	echo 'Dungeon name: <input type="text" name="setDungeonName" size="30" tabindex="1"><br /><br />';
	echo 'Tier: <input type="text" name="setDungeonTier" size="2" maxlength"3" tabindex="2">&nbsp;&nbsp;';
	echo 'Game: <select name="setDungeonGame" tabindex="3">';
		foreach ($gamesArray as $game) {
			echo '<option value="'.$game[gameID].'" selected >'.$game[gameName].'</option>';
		}
	echo '</select><br /><br />';
	echo '<input type="submit" value="Submit" tabindex="4"></form><br />';
}
// -------------------------------------------------------------
function addDungeon()
{
	$formattedName = str_replace("'", "\'", $_POST["setDungeonName"]);
 	global $db, $pun_user;
	$db->query("
			INSERT INTO  
				dmzdkp_dungeons (Name, Tier, gameID) 
			VALUES 
				('".$formattedName."', ".$_POST["setDungeonTier"].", ".$_POST["setDungeonGame"].")
				;") 
		or error('Unable to add dungeon', __FILE__, __LINE__, $db->error()); 
//	function addLogEntry($class, $code, $decode, $value, $reference, $title, $author, $authoriser, $start_date, $end_date)
//	addLogEntry('dkpLEditRd', 'Add', $unixDate, $dungeon, $tier, '', $pun_user['username'], '', 'now', '');
}
// -------------------------------------------------------------
function addMob()
{

}
// -------------------------------------------------------------
function manageGames($gamesArray, $dungeonArray)
{
	foreach ($gamesArray as $game)
	{
		echo '<p><b>'.$game[gameID].' - '.$game[gameName].'</b></p>';
		foreach ($dungeonArray as $dungeon)
		{
			if ($dungeon[dungeonGameID] == $game[gameID])
				echo '&nbsp;&nbsp;<a href="" title="Edit dungeon (disabled)"><img src="./images/common/edit.png" /></a>&nbsp;<img src="./site/dkp/images/instances/tier'.$dungeon[dungeonTier].'.gif" title="Tier '.$dungeon[dungeonTier].'"/>&nbsp;<a href="index.php?page=dkpmanage&d='.$dungeon[dungeonID].'">'.$dungeon[dungeonName].'</a><br />';
		}
	}
	echo '<a href="/index.php?page=dkpmanage&action=setDungeon" title="Add new dungeon"><img src="./images/common/add.png" /></a> <a href="/index.php?page=dkpmanage&action=setDungeon" title="Add new dungeon">Add new dungeon</a>';
}
// -------------------------------------------------------------
function manageMobs($dungeonID, $dungeonname, $mobsArray, $itemsArray)
{
	echo '<p style="text-align: right;"><a href="index.php?page=dkpmanage">Return to dungeon list</a></p>';
//	if ($get['action'] == 'addMob') {
		echo '<form method="post" action="index.php?page=dkpmanage&d='.$dungeonID.'" name="addMob">';
		echo 'Mob name: <input type="text" name="setMobName" size="30" tabindex="1"><br /><br />';
		echo '<input type="submit" value="Submit" tabindex="2"></form><br />';
		echo '<div style="margin: 2px 4px 4px 4px; padding: 5px 0px 8px 0px; border-top: 1px solid #CCC;"></div>';
//	}
	echo '<p><b><u>'.$dungeonID.' - '.$dungeonname.'</u></b>&nbsp;<a href="/index.php?page=dkpmanage&action=addMob&d=8" title="Add new mob"><img src="./images/common/add.png" /></a></p>';
	echo '<div id="accordion">';
	$firstElement = true;
	foreach ($mobsArray as $mob)
	{
		if ($mob[mobDungeonID] == $dungeonID)
		{
			if (!$firstElement)
				echo '</div>';
			echo '&nbsp;&nbsp;<a href="#" class="toggler"><b>'.$mob[mobName].'</b>&nbsp;<a href="" title="Edit mob (disabled)"><img src="./images/common/edit.png" /></a>&nbsp;<a href="http://dmz-gaming.com/index.php?page=dkpmanage&d='.$dungeonID.'&addItem='.$mob[mobID].'" title="Add new item (disabled)"><img src="./images/common/add.png" /></a></a><br /><div class="element">';
			$firstElement = false;

			foreach ($itemsArray as $item)
			{
				if ($item[mobID] == $mob[mobID])
				{
					if ($item[itemValue] > 0) {
						echo '&nbsp;&nbsp;<a href="" title="Edit item (disabled)"><img src="./images/common/edit.png" /></a>&nbsp;';
						echo getItemDisplay($item[gameItemID], getItemColour($item['gameItemID']), $item[itemImage], $item[itemName], $item[itemValue]);
						echo '<br />';
					}
				}
			}
		}
	}
	echo '</div></div>';
}
// -------------------------------------------------------------
/* -------------------------------------------------------------
 -------------------------------------------------------------*/
?>
